package mapper

import (
	"GinShop/config"
	"GinShop/model"
)

type ShopMapper struct {
	*config.Orm
}

func NewShopMapper() *ShopMapper {
	return &ShopMapper{config.DB}
}

const DEFAULT_RANGE = 5

// 116.31   39.21
// 115.31 - 117.31   38.21- 40.21

/**
 * 操作数据库查询商铺数据列表
 */
func (shop *ShopMapper) QueryShops(longitude, latitude float64, keyword string) []model.Shop {
	var shops []model.Shop

	if keyword == "" {
		shop.Where(" longitude > ? AND longitude < ? AND latitude > ? AND latitude < ?  AND status = 1 ", longitude-DEFAULT_RANGE, longitude+DEFAULT_RANGE, latitude-DEFAULT_RANGE, latitude+DEFAULT_RANGE).Find(&shops)
	} else {
		shop.Where(" longitude > ? AND longitude < ? AND latitude > ? AND latitude < ? AND name like ？ and status = 1", longitude-DEFAULT_RANGE, longitude+DEFAULT_RANGE, latitude-DEFAULT_RANGE, latitude+DEFAULT_RANGE, keyword).Find(&shops)
	}
	return shops
}

/**
 * 根据商户id查询对应的服务
 */
func (shop *ShopMapper) QueryServiceByShopId(shopId int64) []model.Service {
	var services []model.Service
	shop.Table("service").Joins("left join shop_service on shop_service.service_id = service.id").Where("shop_service.shop_id", shopId).Find(&services)

	return services
}
